package com.bifang.module.file.core.util;

import com.alicp.jetcache.Cache;
import com.alicp.jetcache.anno.CacheType;
import com.bifang.engine.cache.jetcache.utils.JetCacheUtils;
import com.bifang.engine.cache.redis.RedisHelper;
import com.bifang.module.file.common.cache.FileCacheConst;
import com.bifang.module.file.common.vo.fileAppRead.FileAppReadVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.time.Duration;
import java.util.Set;

/** 清除缓存工具类 */
@Component
public class FileClearCacheUtil {
    @Autowired
    RedisHelper redisHelper;

    /** 文件分类 */
    public void clearFileClassInfo(String fileClassCode, Long appId) {
        if (StringUtils.isNotBlank(fileClassCode)) {
            Cache cache =JetCacheUtils.create(FileCacheConst.FILECLASSINFO, CacheType.BOTH, Duration.ofSeconds(86400),Duration.ofSeconds(300));
            if (cache!=null){
                cache.remove(fileClassCode);
            }
        }
        if (appId != null&&StringUtils.isNotBlank(fileClassCode)) {
            Cache cache = JetCacheUtils.create(FileCacheConst.FILECLASSINFOAPPID, CacheType.BOTH, Duration.ofSeconds(86400),Duration.ofSeconds(300));
            if (cache!=null){
                String key = appId + ":" + fileClassCode;
                cache.remove(key);
            }

        }
    }

    /** 文件分类所有 */
    public void clearFileClassInfo() {
        String[] k = {
                FileCacheConst.FILECLASSINFO,
                FileCacheConst.FILECLASSINFOAPPID
        };
        for(int i=0;i< k.length;i++){
            Set<String> infoKeys = redisHelper.getKeys(k[i]);
            Cache cache = JetCacheUtils.create(k[i],CacheType.BOTH, Duration.ofSeconds(86400),Duration.ofSeconds(300));
            if(cache!=null){
                if(!CollectionUtils.isEmpty(infoKeys)){
                    for(String key : infoKeys){
                        cache.remove(key.replaceAll(k[i],""));
                    }
                }
            }
        }
    }

    public void clearFileRead(String fileClassCode, Long appId){
        if (appId != null&&StringUtils.isNotBlank(fileClassCode)) {
            Cache<String, FileAppReadVO> cache = JetCacheUtils.create(FileCacheConst.FILEREADINFOAPPID, CacheType.BOTH, Duration.ofSeconds(86400),Duration.ofSeconds(300));
            if (cache!=null){
                String key = appId + ":" + fileClassCode;
                cache.remove(key);
            }

        }
    }
    /** 文件服务环境 */
    public void clearFileEnvInfo(Long fileEnvId) {
        if (fileEnvId != null) {
            JetCacheUtils.create(FileCacheConst.FILEENVINFO, CacheType.BOTH, Duration.ofSeconds(86400),Duration.ofSeconds(300)).remove(fileEnvId.toString());
        }
    }

    /** 文件服务环境 */
    public void clearFileEnvInfo() {
        Set<String> infoKeys = redisHelper.getKeys(FileCacheConst.FILEENVINFO);
        Cache cache = JetCacheUtils.create(FileCacheConst.FILEENVINFO,CacheType.BOTH);
        if(cache!=null){
            if(!CollectionUtils.isEmpty(infoKeys)){
                for(String key : infoKeys){
                    cache.remove(key.replaceAll(FileCacheConst.FILEENVINFO,""));
                }
            }
        }
    }
}
